<?php 
namespace Admin\Model;
use Think\Model;

class GoodsModel extends Model
{
	protected $_validate=array(
			['goods','/[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u','商品名称不符合要求'],
			['company','/[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u','厂家名称不符合要求'],
			['price','/^[1-9][0-9]*/','商品价格只能是数字'],
			['store','/^[1-9][0-9]*/','商品价格只能是数字'],
			['descr','/[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u','简介不符合要求'],
		);

	public function index()
	{
		$this->display();
	}
	/**
	 * [goods_add 获取商品类表的信息]
	 * @return [type] [返回获取商品类的表的数据]
	 */
	public function goods_add()
	{
		//获取用户搜索的关键字
		$key = I('get.key');
		$state = I('get.state');
		if(!empty($key)){
			$where['goods'] = ['like',"%$key%"];
		}

		if(!empty($state)){
			$where['status'] = ['eq',$state];
		}

		//统计表中总条数
		$total = $this->where($where)->count();
		//实例化tp中的分页类
		$page = new \Think\Page($total,10);
		//返回显示的分页栏目
		$list['show'] = $page->show();
		//根据关键字进行搜索并将结果返回控制器
		$list['goods'] = $this->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		//查找type表中的数据并返回
		$sql = "select * from zx_type order by concat(path,id)";
		$list['list'] = $this->query($sql);
		
		return $list;
	}
	/**
	 * [goods_insert 添加上传的商品信息到数据库]
	 * @param  [type] $img [获取上传图片后的图片名]
	 * @return [type]      [对用户输入的信息进行判断，不合法则返回，合法则进行插入到数据库中]
	 */
	public function goods_insert($img)
	{
		$data = I('post.');
		$data['pic'] = $img;
		$data['addtime'] = time();	//获取添加的时间点

		//将商品表里面的商品系列转换成type表中的name系列名
		$map['id'] = ['eq',$data['typeid']];
		$tyname = $this->table('zx_type')->where($map)->find();
		$data['typeid'] = $tyname['name'];


		if(!$this->create($data)){	//检测用户输入的信息
			return $this->getError();	
		}else{
			$res = $this->add();	//插入数据
			if(!$res){
				return "插入数据库失败！";
			}else{
				return "success";
			}
		}
	}
	/**
	 * [goods_eidt 查找要修改的商品信息]
	 * @return [type] [返回需要修改的商品信息]
	 */
	public function goods_eidt()
	{
		$data = I('get.');
		$map['id'] = ['eq',$data['id']];
		$sql = "select * from zx_type order by concat(path,id)";
		$list['list'] = $this->query($sql);		//获取商品分类表中的name字段

		$list['goods'] = $this->where($map)->find();
		return $list;
	}
	/**
	 * [goods_updata 更新商品详情表的数据]
	 * @param  [type] $img [如果有上传图片则接收上传后的图片名]
	 * @return [type]      [返回成功与否]
	 */
	public function goods_updata($img)
	{
		$data = I('post.');

		$map['id'] = ['eq',$data['typeid']];
		$tyname = $this->table('zx_type')->where($map)->find();
		$data['typeid'] = $tyname['name'];

		if($img==""){
			$data['pic'] = I('post.oldpicname');
		}else{
			$data['pic'] = $img;
		}


		if(!$this->create($data)){	//检测用户输入的信息
			return $this->getError();	
		}else{
			$res = $this->save();
			return 'success';
		}
	}

	/**
	 * [goods_del 对商品详情表进行删除]
	 * @return [type] [返回拼接后的图片路径，在控制器中进行删除图片操作]
	 */
	public function goods_del()
	{
		$id = I('get.id');
		$map['id'] = ['eq',$id];

		$list = $this->where($map)->find();
		
		$m1 = M('param');		//实例化表param，以便删除
		$m2 = M('norms');
		$mp['gid'] = ['eq',$id];
		$res = $m1->where($mp)->delete();
		$res = $m2->where($mp)->delete();

		
		$oldpicname = "./Uploads/comm/". $list['pic'];
		$del['big'] = $oldpicname;

		$res = $this->where($map)->delete();
		if($res){
			return $del;	//返回需要删除图片的路径
		}else{
			return 'over';
		}
	}

	/**
	 * [goods_img 根据传过来的gid对数据库进行搜索查询]
	 * @return [type] [返回数据集]
	 */
	public function goods_img()
	{
		$gid = I('get.id');
		$map['gid']=['eq',$gid];

		$list['list'] = $this->table('zx_img')->where($map)->select();
		$list['num'] = count($list['list']);
		return $list;
	}


	/*public function goods_page()
	{
		$key = I('get.key');

		if(!empty($key)){
			$where['name']
		}
	}*/
}